<?php
	class Admincp_Model_Product extends Zend_Db_Table_Abstract
	{
		protected $_name = 'product';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem()
        {
            $product = array(
                'p.id',
                'p.name',
                'p.thumbnail',
                'p.price',
                'p.status',
            );
            $provider = array(
                'pv.name AS provider_name',
            );
            $category = array(
                'c.name AS category_name',
            );
            $sql = $this->_db->select()
                            ->from('product AS p', $product)
                            ->joinLeft('provider AS pv', 'p.provider_id = pv.id', $provider)
                            ->joinLeft('category AS c', 'p.category_id = c.id', $category)
                            ->order('p.id DESC');
            return $sql;
        }
        
        public function getItem($id)
        {
            $data = array(
                'name',
                'image',
                'thumbnail',
                'code',
                'price',
                'price_vat',
                'size',
                'description',
                'provider_id',
                'category_id',
            );
            $sql = $this->_db->select()
                            ->from($this->_name, $data)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function saveItem($data, $option)
        {
            if($option == 'create') {
                $this->_db->insert($this->_name, $data);
            }
            else if($option == 'update') {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update($this->_name, $data, $where);
            }
        }
        
        public function deleteItem($id)
        {
            $id = (int) $id;
            $result = $this->getImageById($id);
            if(is_file(PUBLIC_PATH . '/files/product/' . $result['image'])) {
                @unlink(PUBLIC_PATH . '/files/product/' . $result['image']);
            }
            if(is_file(PUBLIC_PATH . '/files/product/thumbnail/' . $result['thumbnail'])) {
                @unlink(PUBLIC_PATH . '/files/product/thumbnail/' . $result['thumbnail']);
            }
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
		    $this->_db->delete($this->_name, $where);
        }
        
        public function deleteMultiItem($listId)
        {
            foreach($listId as $value) {
                $this->deleteItem($value);
            }
        }
        
        public function getImageById($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('image','thumbnail'))
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function searchItem($data)
		{
		    $product = array(
                'p.id',
                'p.name',
                'p.thumbnail',
                'p.price',
                'p.status',
            );
            $provider = array(
                'pv.name AS provider_name',
            );
            $category = array(
                'c.name AS category_name',
            );
            $sql = $this->_db->select()
                            ->from('product AS p', $product)
                            ->joinLeft('provider AS pv', 'p.provider_id = pv.id', $provider)
                            ->joinLeft('category AS c', 'p.category_id = c.id', $category);
		    if($data['name'] != '') {
		        $sql = $sql->where('p.url LIKE ?', $data['name']);
		    }
            if($data['category_id'] != '') {
                $sql = $sql->where('p.category_id = ?', $data['category_id']);
            }
            if($data['provider_id'] != '') {
                $sql = $sql->where('p.provider_id = ?', $data['provider_id']);
            }
		    if($data['start_price'] !== '') {
		        $sql = $sql->where('p.price >= ?', $data['start_date']);
		    }
		    if($data['end_price'] !== '') {
		        $sql = $sql->where('p.price <= ?', $data['end_date']);
		    }
		    if($data['status'] != 0) {
		        $sql = $sql->where('p.status = ?', $data['status']);
		    }
		    $sql = $sql->order('name ASC');
		    return $sql;
		}
    }
